import pandas as pd
import numpy as np
import sklearn.datasets as datasets
from sklearn.svm import SVC
from sklearn.svm import LinearSVC
import matplotlib.pyplot as plt

iris = datasets.load_iris()
x_train = iris.data[:,:2]
y_train = iris.target

x_min,x_max = x_train[:,0].min()-1,x_train[:,0].max()+1
y_min,y_max = x_train[:,1].min()-1,x_train[:,1].max()+1
x,y = np.arange(x_min,x_max,0.01),np.arange(y_min,y_max,0.01)
xx,yy = np.meshgrid(x,y)
xy_test = np.c_[xx.ravel(),yy.ravel()]


estimator = [SVC(kernel='linear'),
             SVC(kernel='rbf'),
             SVC(kernel='poly'),
             LinearSVC()]

from matplotlib.colors import ListedColormap
titles = ['linear_svc','rbf_svc','poly_svc','LinerSVC']

plt.figure(figsize=(12,9))
color = ListedColormap(['#FFAAAA','#AAFFAA','#AAAAFF'])
for i,svc in enumerate(estimator):
    svc.fit(x_train,y_train)
    y_new = svc.predict(xy_test)
    z = y_new.reshape(yy.shape)
    plt.subplot(2,2,(i+1))
    plt.contourf(xx,yy,z,cmap="cool",alpha=0.3) #contourf morenjinxingyansetianchong
    plt.title(titles[i])
    plt.scatter(x_train[:,0],x_train[:,1],cmap=color,c=y_train)
plt.savefig('3svm.png')

